package com.ebeitech.doorapp.domain;

import com.ebeitech.doorapp.db.annotation.Column;
import com.ebeitech.doorapp.db.annotation.Id;
import com.ebeitech.doorapp.db.annotation.PrimaryKey;
import com.ebeitech.doorapp.db.annotation.Table;
import com.ebeitech.doorapp.http.model.BaseModel;

import org.json.JSONObject;

import java.io.Serializable;

/**
 * Created by alice on 2017/10/24.
 */
@Table(name = "UserInfoTable")
public class User extends BaseModel implements Serializable {

	private int _id;

	private String userId;       //用户ID
	private String userAccount;  //用户账号
	private String userName;     //用户名
	private String userAvatar;   //用户头像
	private String password;     //密码
	private String userSig;      //腾讯云登陆令牌
	private String token;        //Token

	private String projectId;     //项目ID
	private String projectName;   //项目名字

	private String loginStatus;  //登陆状态
	private String lastLoginTime;//上次登录时间

	@Id
	@Column(name = "_id")
	public int get_id() {
		return _id;
	}

	public void set_id(int _id) {
		this._id = _id;
	}

	@PrimaryKey
	@Column(name = "userId")
	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	@Column(name = "userAccount")
	public String getUserAccount() {
		return userAccount;
	}

	public void setUserAccount(String userAccount) {
		this.userAccount = userAccount;
	}

	@Column(name = "userName")
	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	@Column(name = "userAvatar")
	public String getUserAvatar() {
		return userAvatar;
	}

	public void setUserAvatar(String userAvatar) {
		this.userAvatar = userAvatar;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	@Column(name = "userSig")
	public String getUserSig() {
		return userSig;
	}

	public void setUserSig(String userSig) {
		this.userSig = userSig;
	}

	@Column(name = "token")
	public String getToken() {
		return token;
	}

	public void setToken(String token) {
		this.token = token;
	}

	@Column(name = "projectId")
	public String getProjectId() {
		return projectId;
	}

	public void setProjectId(String projectId) {
		this.projectId = projectId;
	}

	@Column(name = "projectName")
	public String getProjectName() {
		return projectName;
	}

	public void setProjectName(String projectName) {
		this.projectName = projectName;
	}

	@Column(name = "loginStatus", defaultValue = "0")
	public String getLoginStatus() {
		return loginStatus;
	}

	public void setLoginStatus(String loginStatus) {
		this.loginStatus = loginStatus;
	}

	@Column(name = "lastLoginTime")
	public String getLastLoginTime() {
		return lastLoginTime;
	}

	public void setLastLoginTime(String lastLoginTime) {
		this.lastLoginTime = lastLoginTime;
	}

	public void initWithJson(String jsonStr) {
		User user = this;
		try {
			JSONObject jsonObject = new JSONObject(jsonStr);
			user.setUserId(jsonObject.optString("id"));
			user.setUserAccount(jsonObject.optString("account"));
			user.setUserName(jsonObject.optString("name"));
			user.setUserAvatar(jsonObject.optString("avatar"));
			user.setPassword(jsonObject.optString("password"));
			user.setUserSig(jsonObject.optString("sig"));

			JSONObject company = jsonObject.getJSONObject("company");
			user.setProjectId(company.optString("id"));
			user.setProjectName(company.optString("name"));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
